package com.fengwk.support.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.fengwk.support.exception.BaseException;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

/**
 * 根据类:缩略图工具
 */
public class ThumbnailatorUtils {

	private ThumbnailatorUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	/**
	 * 缩略图
	 * 
	 * @param is
	 * @param os
	 * @param scaleW 缩略宽,必须与scaleH同时使用
	 * @param scaleH 缩略高,必须与scaleW同时使用
	 * @param quality 压缩质量
	 * @throws IOException
	 */
	public static void thumbnail(InputStream is, OutputStream os, Double scaleW, Double scaleH, Float quality) throws IOException {
		Builder<?> b = Thumbnails.of(is);
		if (scaleW == null && scaleH != null || scaleW != null && scaleH == null)
			throw new BaseException("scaleW and scaleH must be both null or not null");
		if (scaleW == null)
			scaleW = 1d;
		if (scaleH == null)
			scaleH = 1d;
		if (quality == null)
			quality = 1f;
		b.scale(scaleW, scaleH);
		b.outputQuality(quality);
		try {
			b.toOutputStream(os);
		} finally {
			IoUtils.close(is);
			IoUtils.close(os);
		}
	}
	
	/**
	 * 缩略图
	 * 
	 * @param is
	 * @param os
	 * @param scale
	 * @throws IOException
	 */
	public static void thumbnail(InputStream is, OutputStream os, double scale) throws IOException {
		thumbnail(is, os, scale, scale, null);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inFile
	 * @param outFile
	 * @param scale
	 * @throws IOException
	 */
	public static void thumbnail(File inFile, File outFile, double scale) throws IOException {
		thumbnail(IoUtils.buildInputStream(inFile), IoUtils.buildOutputStream(outFile), scale);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inPath
	 * @param outPath
	 * @param scale
	 * @throws IOException
	 */
	public static void thumbnail(String inPath, String outPath, double scale) throws IOException {
		thumbnail(IoUtils.buildInputStream(inPath), IoUtils.buildOutputStream(outPath), scale);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param is
	 * @param os
	 * @param scaleW
	 * @param scaleH
	 * @throws IOException
	 */
	public static void thumbnail(InputStream is, OutputStream os, double scaleW, double scaleH) throws IOException {
		thumbnail(is, os, scaleW, scaleH, null);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inFile
	 * @param outFile
	 * @param scaleW
	 * @param scaleH
	 * @throws IOException
	 */
	public static void thumbnail(File inFile, File outFile, double scaleW, double scaleH) throws IOException {
		thumbnail(IoUtils.buildInputStream(inFile), IoUtils.buildOutputStream(outFile), scaleW, scaleH);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inPath
	 * @param outPath
	 * @param scaleW
	 * @param scaleH
	 * @throws IOException
	 */
	public static void thumbnail(String inPath, String outPath, double scaleW, double scaleH) throws IOException {
		thumbnail(IoUtils.buildInputStream(inPath), IoUtils.buildOutputStream(outPath), scaleW, scaleH);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param is
	 * @param os
	 * @param quality
	 * @throws IOException
	 */
	public static void thumbnail(InputStream is, OutputStream os, float quality) throws IOException {
		thumbnail(is, os, null, null, quality);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inFile
	 * @param outFile
	 * @param quality
	 * @throws IOException
	 */
	public static void thumbnail(File inFile, File outFile, float quality) throws IOException {
		thumbnail(IoUtils.buildInputStream(inFile), IoUtils.buildOutputStream(outFile), quality);
	}
	
	/**
	 * 缩略图
	 * 
	 * @param inPath
	 * @param outPath
	 * @param quality
	 * @throws IOException
	 */
	public static void thumbnail(String inPath, String outPath, float quality) throws IOException {
		thumbnail(IoUtils.buildInputStream(inPath), IoUtils.buildOutputStream(outPath), quality);
	}
	
}

